<?php

namespace MoChat\App\WorkContact\Action\Dashboard;

use Hyperf\DbConnection\Db;
use MoChat\App\Common\Exception\CommonException;
use MoChat\Framework\Action\AbstractAction;
use MoChat\Framework\Request\ValidateSceneTrait;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\Middleware;
use Hyperf\HttpServer\Annotation\Middlewares;
use Hyperf\HttpServer\Annotation\RequestMapping;
use MoChat\App\Common\Middleware\DashboardAuthMiddleware;
use MoChat\App\Rbac\Middleware\PermissionMiddleware;

/**
 * @Controller
 */
class ClientSelect extends AbstractAction
{
    use ValidateSceneTrait;

    /**
     * @Middlewares({
     *     @Middleware(DashboardAuthMiddleware::class),
     * })
     * @RequestMapping(path="/dashboard/workContact/clientSelect", methods="GET")
     */
    public function handle()
    {
        $params = $this->request->all();
        $list = Db::table('client')
            ->when(isset($params['keyword']) && !empty($params['keyword']),function ($query)use($params){
                $query->where('enterprise_name', 'like', '%' . $params['keyword'] . '%');
            })
            ->limit(100)->select([
            'enterprise_name as name', 'id'
        ])->get()->toArray();
        $list || $list = collect([]);
        return empty($list) ? [] :$list ;
    }

}
